
class UserSerializer(serializers.ModelSerializer):
    """用户序列化器类"""
    password2 = serializers.CharField(label='重复密码',write_only=True)
    sms_code = serializers.CharField(label='短信验证码',write_only=True)
    allow = serializers.CharField(label='同意协议',write_only=True)

    class Meta:
        model = User
        fields = ('id','username','password','mobile','password2','sms_code','allow')
        # 可以额外补充参数
        extra_kwargs = {
            'username': {
                'min_length': 5,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许5-20个字符的用户名',
                    'max_length': '仅允许5-20个字符的用户名',
                }
            },
            'password': {
                'write_only': True,
                'min_length': 8,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许8-20个字符的密码',
                    'max_length': '仅允许8-20个字符的密码',
                }
            }
        }

        def validate_mobile(self, value):
            """验证手机号"""
            if not re.match(r'^1[3-9]\d{9}$', value):
                raise serializers.ValidationError('手机号格式错误')

            # 手机号是否重复
            count = User.objects.filter(mobile=value).count()
            if count > 0:
                raise serializers.ValidationError('手机号已存在')

            return value

        def validate_allow(self, value):
            """检验用户是否同意协议"""
            if value != 'true':
                raise serializers.ValidationError('请同意用户协议')
            return value

        def validate(self, data):
            # 判断两次密码
            if data['password'] != data['password2']:
                raise serializers.ValidationError('两次密码不一致')

            # 判断短信验证码
            redis_conn = get_redis_connection('verify_codes')
            mobile = data['mobile']
            real_sms_code = redis_conn.get('sms_%s' % mobile)
            if real_sms_code is None:
                raise serializers.ValidationError('无效的短信验证码')
            #注意：前端传过来的是str，而redis里面保存的是bytes类型数据
            if data['sms_code'] != real_sms_code.decode():
                raise serializers.ValidationError('短信验证码错误')

            return data

        def create(self, validated_data):
            """
            创建用户
            """
            # 移除数据库模型类中不存在的属性
            del validated_data['password2']
            del validated_data['sms_code']
            del validated_data['allow']

            # 创建新用户
            user = User.objects.create_user(**validated_data)

            return user
